package com.jh.user.repository;

import java.math.BigDecimal;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.jh.user.pojo.UserRebateHistory;

@Repository
public interface UserRebateHistoryRepository extends JpaRepository<UserRebateHistory,String>,JpaSpecificationExecutor<UserRebateHistory>{


	@Query("select rebateHistory from  UserRebateHistory rebateHistory where rebateHistory.userId=:userid ")
	Page<UserRebateHistory> findRebateHistoryByUserid(@Param("userid") long userid,Pageable pageAble);

	@Query("select sum(rebateHistory.rebate) from  UserRebateHistory rebateHistory where rebateHistory.userId=:userid and rebateHistory.orderType in (:orderType) and rebateHistory.addOrSub=0 and rebateHistory.createTime>:startTime and  rebateHistory.createTime<:endTime")
	BigDecimal findsumRebateHistoryByUseridAndTime(@Param("userid") long userid,@Param("orderType") String[]  orderType, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

	//注销用户历史记录
	@Modifying
	@Query("delete from UserRebateHistory rebateHistory where rebateHistory.userId=:userid")
	void delUserRebateHistoryByUserid(@Param("userid") long userid);

	@Query("select rebateHistory.createDate,sum(rebateHistory.rebate) from UserRebateHistory rebateHistory where rebateHistory.userId=:userId and rebateHistory.createDate >=:startDate and rebateHistory.orderType in (:orderType) group by rebateHistory.createDate ")
	List<Object[]> findSumRebateHistoryByUserIdBetweenWeek(@Param("userId")long userId,@Param("orderType")String[] orderType,@Param("startDate") String startTime);
}
